package com.webmagic.demo;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.FileCacheQueueScheduler;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import us.codecraft.webmagic.scheduler.RedisScheduler;

/**
 * Created by fangjicai on 2020/6/13.
 * 爬虫类
 */
public class MyProcessor2 implements PageProcessor {

    public void process(Page page) {
        //page.addTargetRequests( page.getHtml().links().all() );//将当前页面里的所有链接都添加到目标页面中
//
        page.addTargetRequests(
                page.getHtml().links().regex("https://blog.csdn.net/[a-z 0-9-]+/article/details/[0-9]{9}").all() );
//System.out.println(page.getHtml().xpath("//*[@id=\"mainBox\"]/main/div[1]/div[1]/h1/text()").toString());
        page.putField("title",page.getHtml().xpath("//* [@id=\"mainBox\"]/main/div[1]/div[1]/h1/text()").toString());
    }
    public Site getSite() {
        return Site.me().setSleepTime(100).setRetryTimes(3);
    }
/*    public static void main(String[] args) {
        Spider.create( new MyProcessor() )
                .addUrl("https://blog.csdn.net")
                .addPipeline(new ConsolePipeline())
                .run();
    }*/
//public static void main(String[] args) {
//    Spider.create( new MyProcessor() )
//            .addUrl("https://blog.csdn.net")
//            .addPipeline(new ConsolePipeline())
//            .addPipeline(new FilePipeline("e:/data"))//以文件方式保存
//            .run();
//}

//    public static void main(String[] args) {
//        Spider.create( new MyProcessor() )
//                .addUrl("https://blog.csdn.net")
//                .addPipeline(new ConsolePipeline())
//                .addPipeline(new FilePipeline("e:/data"))
//                .addPipeline(new JsonFilePipeline("e:/json"))// 以json方式保
//                .run();
//    }

//    public static void main(String[] args) {
//        Spider.create( new MyProcessor() )
//                .addUrl("https://blog.csdn.net")
//                .addPipeline(new ConsolePipeline())
//                .addPipeline(new FilePipeline("e:/data"))
//                .addPipeline(new JsonFilePipeline("e:/json"))
//                .addPipeline(new MyPipeline())//定制化输出
//                .run();
//    }

//    public static void main(String[] args) {
//        Spider.create( new MyProcessor() )
//                .addUrl("https://blog.csdn.net")
//                .setScheduler(new QueueScheduler())
//                .run();
//    }

//    public static void main(String[] args) {
//        Spider.create( new MyProcessor() )
//                .addUrl("https://blog.csdn.net")
////.setScheduler(new QueueScheduler())//设置内存队列
//                .setScheduler(new
//                        FileCacheQueueScheduler("E:\\scheduler"))//设置文件队列
//                .run();
//    }

    public static void main(String[] args) {
        Spider.create( new MyProcessor() )
                .addUrl("https://blog.csdn.net")
//.setScheduler(new QueueScheduler())//设置内存队列
//.setScheduler(new FileCacheQueueScheduler("E:\\scheduler"))//设置文件队列
         .setScheduler(new RedisScheduler("127.0.0.1"))//设置Redis队列
                .run();
    }

}
